﻿Shader "Dan/UI/UI_SphereRotation"
{
    Properties
    {
        _MainTex ("MainTex", 2D) = "white" {}        
        _ColorOutside ("ColorOutside", Color) = (0.07843138,0.3921569,0.7843137,1)
        _ColorInside ("ColorInside", Color) = (1,0,0,1)
        _ColorRampContrast ("ColorRampContrast", Range(0, 1)) = 0.5
        _Emission ("Emission", Float ) = 1
        _Scale ("Scale", Range(0, 1)) = 1
        _SphereUV ("SphereUV", 2D) = "white" {}
        _SphereSpeed("SphereSpeed", Float ) = 1
        _ScaleSphere("ScaleSphere", Range(0, 1) ) = 1
        _SphereGlow("SphereGlow", Range(0, 1) ) = 1
        _SphereBackOpacity("SphereBackOpacity", Range(0, 1) ) = 0.25
        _Pitch_RGBA("Pitch_RGBA", Vector ) = (0.1,0.8,0.45,0) 
        _Roll_RGBA("Roll_RGBA", Vector ) = (3,3,2.5,2.75)
        _Offset_RGBA("Offset_RGBA", Vector ) = (0,0,0,0)
        _Speed_RGBA("Speed_RGBA", Vector ) = (0.8,-1.25,-1.5,-0.75)
        _PitchSpeed_RGBA("PitchSpeed_RGBA", Vector ) = (0.1,-1.5,0.75,3)
        
        

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _ColorOutside;
             sampler2D _MainTex; 
             sampler2D _SphereUV;
             float _SphereSpeed ;
             float _ScaleSphere;
             float _SphereGlow;
             float _SphereBackOpacity;
             float4 _MainTex_ST;
             float _Scale;
             float4 _Pitch_RGBA;
             float4 _Roll_RGBA;
             float4 _Offset_RGBA;
             float4 _Speed_RGBA;
             float4 _PitchSpeed_RGBA;
             float _ColorRampContrast;
             float4 _ColorInside;
             float _Emission;
             float _FloaSpeed_A;
             float _Amplitude_A;
            
            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
            
            
            
            


            fixed4 frag (v2f i ) : SV_Target
            {
                
                float2 uv = i.uv;
                uv =TRANSFORM_TEX(uv, _MainTex);
                //故障启动
                float2 rd = float2(_Time.y,_Time.y)*0.001;
                float flick = Flick(rd);
                float FadeOut ;
                FadeoutGlitch(_Scale,flick,FadeOut);

                

                float2 pitchUV; float2 uvPitchRoll;//声明需要用到的uv
                float2 rotUV01;float2 rotUV02;float2 rotUV03;
                
                float2 scaleUV =  (_Scale+2*uv-1)/(2*_Scale);//缩放值
                float4 pitchSpeed =_Time.x*_PitchSpeed_RGBA; 
                
                float pitchVal_R = frac(_Pitch_RGBA.x+frac(pitchSpeed.x));
                float pitchVal_G = frac(_Pitch_RGBA.y+frac(pitchSpeed.y));
                float pitchVal_B = frac(_Pitch_RGBA.z+frac(pitchSpeed.z));
               
               
               
                //求各通道图形的UV旋转
                PitchRollUV(uv,pitchVal_R,_Roll_RGBA.x,_Offset_RGBA.x,_Speed_RGBA.x,scaleUV,pitchUV,uvPitchRoll,rotUV01);
                PitchRollUV(uv,pitchVal_G,_Roll_RGBA.y,_Offset_RGBA.y,_Speed_RGBA.y,scaleUV,pitchUV,uvPitchRoll,rotUV02);
                PitchRollUV(uv,pitchVal_B,_Roll_RGBA.z,_Offset_RGBA.z,_Speed_RGBA.z,scaleUV,pitchUV,uvPitchRoll,rotUV03);
                
                //色彩渐变
                float colorRamp = saturate(length(scaleUV*1.5-0.75) /_ColorRampContrast);
                float3 finalColor = lerp(_ColorInside.rgb,_ColorOutside.rgb,colorRamp)*_Emission;
                 
                
                float channelR = tex2D(_MainTex,rotUV01).r;
                float channelG = tex2D(_MainTex,rotUV02).g;
                float channelB = tex2D(_MainTex,rotUV03).b;
                
                //球形滚动
                float sphereRotation =SphereRotate(uv,_ScaleSphere*_Scale,_SphereGlow,_SphereSpeed,_SphereBackOpacity,_SphereUV,_MainTex);
                
                
                
                return fixed4(finalColor,(saturate(sphereRotation+channelR+channelG+channelB)*FadeOut));
            
                
               
                

                


                
                
            }
            ENDCG
        }
    }
    
    
    
}
